package common

import (
	"github.com/dgrijalva/jwt-go"
	"test/model"
	"time"
)

var jwtkey = []byte("a_secret_crect")


type Claims struct {
	UserId uint
	jwt.StandardClaims
}

func ReleseToken(stu model.Students) (string,error) {
	expirationTime := time.Now().Add(7 * 24 * time.Hour)
	claims := &Claims{
		UserId: uint(stu.Sno),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: expirationTime.Unix(),
			IssuedAt: time.Now().Unix(),
			Issuer: "oceanlearn.tech",
			Subject: "people token",
		},
	}

	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	tokenString,err := token.SignedString(jwtkey)
	if err != nil {
		return "", err
	}

	return tokenString,nil
}

func ParseToken(tokenString string) (*jwt.Token,*Claims,error){//解析
	claims := &Claims{}

	token,err := jwt.ParseWithClaims(tokenString, claims,func(token *jwt.Token) (i interface{},err error) {
		return jwtkey,nil
	})

	return token,claims,err
}
